package com.example.wm.SanWar.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.SanWar.entity.ApplyForExchange;
import com.example.wm.SanWar.entity.PrizeManagement;
import com.example.wm.SanWar.mapper.ApplyForExchangeMapper;
import com.example.wm.SanWar.service.PrizeManagementService;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.SanWar.VO.PrizeManagementfindVo;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/PrizeManagement")
public class PrizeManagementController {
    @Resource
    private TsyUtils tsyUtils;
    @Resource
    private ApplyForExchangeMapper applyForExchangeMapper;
    @Resource
    private PrizeManagementService prizeManagementService;
    @Transactional
    @PostMapping("/Insert")
    public Result Insert(@RequestBody PrizeManagement prizeManagement) {
       prizeManagement.setCreatetime(tsyUtils.getnowdate());
        boolean b=prizeManagementService.save(prizeManagement);
        return ResultData.success(b);
    }
    @Transactional
    @PostMapping("/findbyname")
    public Result findbyname(@RequestBody JSONObject jsonObject) {
        String type=jsonObject.getString("type");
         LambdaQueryWrapper<PrizeManagement> lq=new LambdaQueryWrapper<>();
         lq.eq(PrizeManagement::getCreatenameid,jsonObject.getString("createnameid"));
         lq.eq(PrizeManagement::getStatuss,jsonObject.getString("statuss"));

         List<PrizeManagement> prizeManagementList = prizeManagementService.list(lq);
        ArrayList<PrizeManagementfindVo> prizeManagementfindVoList=new ArrayList<>();
       if(!prizeManagementList.isEmpty()){
           for (PrizeManagement pm : prizeManagementList){
               LambdaQueryWrapper<ApplyForExchange> lq1=new LambdaQueryWrapper<>();
               lq1.eq(ApplyForExchange::getApplyraffleid,pm.getId());

               if(Objects.equals(type, "待审核") ){
                   lq1.ne(ApplyForExchange::getStatus,"未通过");
                   lq1.ne(ApplyForExchange::getStatus,"未中奖");
                   lq1.eq(ApplyForExchange::getStatus,"待审核");

               }else if(Objects.equals(type, "待发货") ){
                   lq1.ne(ApplyForExchange::getStatus,"未通过");
                   lq1.ne(ApplyForExchange::getStatus,"未中奖");
                   lq1.eq(ApplyForExchange::getStatus,"待发货");

               }else
               {
                   lq1.ne(ApplyForExchange::getStatus,"未通过");
                   lq1.ne(ApplyForExchange::getStatus,"未中奖");
               }

               List<ApplyForExchange> applyForExchangeList = applyForExchangeMapper.selectList(lq1);
               PrizeManagementfindVo pzv=new PrizeManagementfindVo();
               pzv.setPrizeManagement(pm);
               if(!applyForExchangeList.isEmpty() && Objects.equals(type, "待审核")){
                   pzv.setApplyForExchangeList(applyForExchangeList);
                   pzv.setApplynumber(applyForExchangeList.size());
                   prizeManagementfindVoList.add(pzv);
               }
               if(!applyForExchangeList.isEmpty() && Objects.equals(type, "待发货")){
                   pzv.setApplyForExchangeList(applyForExchangeList);
                   pzv.setApplynumber(applyForExchangeList.size());
                   prizeManagementfindVoList.add(pzv);
               }
               if( !Objects.equals(type, "待审核") && !Objects.equals(type, "待发货")){
                   pzv.setApplyForExchangeList(applyForExchangeList);
                   pzv.setNumber(applyForExchangeList.size());
                   prizeManagementfindVoList.add(pzv);
               }


           }
           return ResultData.success(prizeManagementfindVoList);

       }else {
           return ResultData.error(202,"无数据");
       }
    }
    @Transactional
    @PostMapping("/update")
    public Result update(@RequestBody PrizeManagement prizeManagement) {
         boolean b=prizeManagementService.updateById(prizeManagement);
         return ResultData.success(b);
    }
    @Transactional
    @PostMapping("/delete")
    public Result delete(@RequestBody PrizeManagement prizeManagement) {
        boolean b=prizeManagementService.removeById(prizeManagement);
        return ResultData.success(b);
    }
    // 检查未完成的信息

    @PostMapping("/check")
    public Result check(@RequestBody PrizeManagement prizeManagement) {
        try {
            // 校验输入参数
            if (prizeManagement == null || prizeManagement.getId() == null) {
                log.warn("Invalid input: prizeManagement or its ID is null");
                return ResultData.error(303,"Invalid input");
            }

            LambdaQueryWrapper<ApplyForExchange> lq = new LambdaQueryWrapper<>();
            lq.eq(ApplyForExchange::getApplyraffleid, prizeManagement.getId());
            lq.in(ApplyForExchange::getStatus, "待审核", "待发货");

            List<ApplyForExchange> applyForExchangeList = applyForExchangeMapper.selectList(lq);
            log.info("Query result size: {}", applyForExchangeList.size());

            return ResultData.success(applyForExchangeList);
        } catch (Exception e) {
            log.error("Error occurred while checking prize management", e);
            return ResultData.error(303,"System error");
        }
    }

}
